/*
 * =====================================================================================
 *
 *       Filename:  myt.cc
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2009年11月12日 13时20分07秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  lerosua (), lerosua@gmail.com
 *        Company:  cyclone
 *
 * =====================================================================================
 */


#include <cassert>
#include <sstream>
#include <string>
#include <fstream>

#include "jsonxx.h"

namespace jsonxx {
bool parse_string(std::istream& input, std::string* value);
bool parse_number(std::istream& input, long* value);
bool match(const std::string& pattern, std::istream& input, bool ignore_ws = true);
}
    using namespace jsonxx;
    using namespace std;


void    test(const std::string& str_content )
{
string value;
	istringstream input(str_content);
	Object o;
        assert(o.parse(input));
	value = o.get<string>("created_at");
	cout<<"create at = "<<value<<endl;

	//long lvalue;
	//lvalue=(o.get<Object>("user").get<long>("id"));
	//cout<<"user id  = "<<lvalue<<endl;

	assert(o.has<Object>("user"));
	value=(o.get<Object>("user").get<string>("screen_name"));
	cout<<"screen_name  = "<<value<<endl;


	long long llvalue;
	llvalue=o.get<long long>("id");
	cout<<"id2  = "<<llvalue<<endl;

	llvalue=(o.get<Object>("user").get<long long>("id"));
	cout<<"user id  = "<<llvalue<<endl;
	value = o.get<string>("text");
	cout<<"text = "<<value<<endl;

	


}


int main(int argc, char *argv[])
{

	string filename="./test.json";
	ifstream infile;
	string str_content;
	infile.open(filename.c_str());

	getline(infile,str_content);

	//cout<<"get json \n"<<str_content<<endl;

#if 0
	vector<string> list;
	
	string str1,str2;
	size_t pos;
	pos= str_content.find(",{");
	if(pos!=std::string::npos)
		str1=str_content.substr(1,pos);
	str2=str_content.substr(pos+1,std::string::npos);
		
	cout<<"str1 =  "<<str1<<endl<<endl;
	cout<<"str2 =  "<<str2<<endl;

	test(str1);
	test(str2);

#endif

		
	string strp;
	string tmp;
	size_t begin_pos=0;
	size_t pos;
	int count=0;
do{
	pos= str_content.find(",{");
	if(pos==std::string::npos)
		break;

	if(0==count)
		strp=str_content.substr(1,pos-1);
	else
		strp=str_content.substr(begin_pos,pos);
	tmp=str_content.substr(pos+1,std::string::npos);

	test(strp);
	//cout<<"str"<<count<<" =  "<<strp<<endl<<endl;
	count++;

	str_content=tmp;
	//begin_pos=pos+1;

}while(true);

	//cout<<"str"<<count<<" =  "<<tmp<<endl<<endl;

	test(tmp);








 
	printf("\n");
	return 0;
}
